﻿using System;
using System.Drawing;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary;

namespace DistributionApplication.Controls
{
    public partial class PositionViewer : UserControl
    {
        private double m_Latitude;
        private double m_Longitude;
        private GPS m_GpsManager;
        private bool m_HasPosition;

        public bool HasPosition
        {
            get
            {
                return m_GpsManager.HasSolution;
            }
        }
        public GPS Gps
        {
            set
            {
                m_GpsManager = value;
            }
        }
        public double Latitude
        {
            get
            {
                return m_Latitude;
            }
            set
            {
                m_Latitude = value;
            }
        }
        public double Longitude
        {
            get
            {
                return m_Longitude;
            }
            set
            {
                m_Longitude = value;
            }
        }
        

        public PositionViewer()
        {
            InitializeComponent();
        }

        private void pbxRefresh_Click(object sender, EventArgs e)
        {
            RefreshPosition();
        }

        public void RefreshPosition()
        {
            if (m_GpsManager.HasSolution)
            {
                string stemp = "";
                m_Latitude = ConvertMobileHelper.ConvertLatitudeToDecimal(m_GpsManager.NmeaGGA.Latitude, m_GpsManager.NmeaGGA.LatitudeHemisphere, ref stemp);
                m_Longitude = ConvertMobileHelper.ConvertLongitudeToDecimal(m_GpsManager.NmeaGGA.Longitude, m_GpsManager.NmeaGGA.LongitudeHemisphere, ref stemp);
            }
            else
            {
                m_Latitude = double.MinValue;
                m_Longitude = double.MinValue;
            }
            SetPosition(m_Latitude, m_Longitude);
        }

        public void SetPosition(double latitude, double longitude)
        {
           
            if(latitude != double.MinValue && longitude != double.MinValue)
            {
                m_Latitude = latitude;
                m_Longitude = longitude;
                lblLocation.Tag = true;
                lblLocation.ForeColor = Color.Black;
                //Long: 103.123456 Lat: 103.123456
                lblLocation.Text = "Long: " + latitude.ToString("F6") + " Lat: " + longitude.ToString("F6");
            }else
            {
                m_Latitude = double.MinValue;
                m_Longitude = double.MinValue;
                lblLocation.Tag = false;
                lblLocation.ForeColor = Color.Red;
                lblLocation.Text = "A GPS Solution is not available";
            }
        }
    }
}
